<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Stage extends Model
{
    //
    protected $table = 'stage';

    protected $casts = [
        'result' => 'json',
    ];

    public function match()
    {
        return $this->belongsTo(Match::class, 'match_id', 'id');
    }

    public static function setWinFail(&$stage, $uid, $type)
    {
        if (!isset($stage['result'][$type])) {
            $user = User::selectNormal()->find($uid);
            $stage['result'][$type] = $user->toArray();
            $t = self::find($stage['id']);
            $t['result'] = $stage['result'];
            $t->save();
        }
        return true;
    }
}
